

package ro.ase.catalog.security.dao.hibernate;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import ro.ase.catalog.beans.Role;
import ro.ase.catalog.dao.hibernate.GenericDAOImpl;
import ro.ase.catalog.security.dao.IRoleDAO;

import java.util.List;


public class RoleDAOImpl extends GenericDAOImpl<Role, Integer>  implements IRoleDAO {

    @SuppressWarnings("unchecked")
    public List<Role> findByNameVaguePerPage(String name, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.add(Expression.like("name", "%"+name+"%"));
        return getHibernateTemplate().findByCriteria(criteria,start,limit);            
    }	
    
    public Integer findByNameVagueCount(String name) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.add(Expression.like("name", "%"+name+"%"));
        criteria.setProjection(Projections.rowCount());
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
    }   
    
    @SuppressWarnings("unchecked")
    public Role findByRolename(String rolename) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.add(Expression.eq("name", rolename));

        List<Role> list = getHibernateTemplate().findByCriteria(criteria);
        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }
    
    @SuppressWarnings("unchecked")
    public List<Role> findByUserIdPerPage(int userId, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.createCriteria("users")
                .add(Expression.eq("id", userId));
        return getHibernateTemplate().findByCriteria(criteria, start, limit);
    }
    
    public Integer findByUserIdCount(int userId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.setProjection(Projections.rowCount())
                .createCriteria("users")
                .add(Expression.eq("id", userId));
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
    }

    @SuppressWarnings("unchecked")
    public List<Role> findByAuthorityIdPerPage(int authorityId, int start, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.createCriteria("authorities")
                .add(Expression.eq("id", authorityId));
        return getHibernateTemplate().findByCriteria(criteria, start, limit);
    }
    
    public Integer findByAuthorityIdCount(int authorityId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
        criteria.setProjection(Projections.rowCount())
                .createCriteria("authorities")
                .add(Expression.eq("id", authorityId));
        return (Integer)getHibernateTemplate().findByCriteria(criteria).get(0);
    }

}
